//
//  PaperSliderView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/3/21.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperSliderView.h"

@interface PaperSliderView ()

@property (nonatomic, strong) UIView *sliderBackView;       // 滑动条背景
@property (nonatomic, strong) UIView *sliderView;           // 滑动条
@property (nonatomic, strong) UIView *sliderCycleView;      // 滑动条圆点
@property (nonatomic, strong) UILabel *numLabel;            // 当前页码

@end

@implementation PaperSliderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.sliderBackView];
    [self addSubview:self.sliderView];
    [self addSubview:self.sliderCycleView];
//    [self addSubview:self.numLabel];
}

#pragma mark - setter

- (void)setTitle:(NSString *)title {
    _title = title;
    self.numLabel.text = title;
}

- (void)setProgress:(CGFloat)progress {
    if (progress < 0.0) {
        progress = 0.0f;
    }
    
    if (progress > 1.0) {
        progress = 1.0f;
    }
    
    _progress = progress;
    [UIView animateWithDuration:0.5 animations:^{
        self.sliderView.width = self.sliderBackView.width * progress;
        self.sliderCycleView.left = self.sliderView.width + self.sliderView.left - 3.0f;
    }];
}

#pragma mark - getter

- (UIView *)sliderBackView {
    if (_sliderBackView == nil) {
        _sliderBackView = [[UIView alloc] initWithFrame:CGRectMake(18.0, 17.0, SCREEN_WIDTH - 36.0, 6.0)];
//        _sliderBackView = [[UIView alloc] initWithFrame:CGRectMake(18.0, 17.0, SCREEN_WIDTH - 24 - 60, 6.0)];
        _sliderBackView.backgroundColor = [UIColor lightGrayColor];
        ViewBorderRadius(_sliderBackView, 3.0, 1.0, [UIColor lightGrayColor]);
    }
    return _sliderBackView;
}

- (UIView *)sliderView {
    if (_sliderView == nil) {
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(18.0, 17.0, 0, 6.0)];
        _sliderView.backgroundColor = MainThemeColor;
        ViewBorderRadius(_sliderView, 3.0, 1.0, MainThemeColor);
    }
    return _sliderView;
}

- (UIView *)sliderCycleView {
    if (_sliderCycleView == nil) {
        _sliderCycleView = [[UIView alloc] initWithFrame:CGRectMake(12, 14, 12, 12)];
        _sliderCycleView.backgroundColor = MainThemeColor;
        ViewBorderRadius(_sliderCycleView, 6.0, 1.0, MainThemeColor);
    }
    return _sliderCycleView;
}

- (UILabel *)numLabel {
    if (_numLabel == nil) {
        _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 63, 0, 60, 40)];
        _numLabel.textColor = [UIColor darkGrayColor];
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.font = [UIFont systemFontOfSize:14.0f];
        _numLabel.text = @"1/10";
    }
    return _numLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
